home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 11496 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  2.2 KB

  1. Path: rcp6.elan.af.mil!rscernix!danpop
  2. From: danpop@mail.cern.ch (Dan Pop)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: problem w/ realloc
  5. Date: 21 Mar 96 20:06:03 GMT
  6. Organization: CERN European Lab for Particle Physics
  7. Distribution: usa
  8. Message-ID: <danpop.827438763@rscernix>
  9. References: <4iqef0$dqq@montana.avicom.net>
  10. NNTP-Posting-Host: ues5.cern.ch
  11. X-Newsreader: NN version 6.5.0 #7 (NOV)
  12.  
  13. In <4iqef0$dqq@montana.avicom.net> zoot@montana.avicom.net (John Greenup) writes:
  14.  
  15. >I've been porting a piece of code from AIX 3.2.5 to linux 1.2.8
  16. >(gcc 2.7.0) recently, and finaly chased down the last :-) bug.  The code 
  17. >assumes that if you have a memory block, and you realloc it smaller, it 
  18. >doesn't move the block of memory, it just makes the "usable size" of it 
  19. >smaller.
  20.  
  21. Such an assumption is silly.  It has nothing to do with the C language
  22. definition, which actually says:
  23.  
  24.     Description
  25.  
  26.     The realloc function changes the size of the object pointed to by
  27.     ptr to the size specified by size .  The contents of the object shall
  28.     be unchanged up to the lesser of the new and old sizes.  If the new
  29.     size is larger, the value of the newly allocated portion of the object
  30.     is indeterminate.  If ptr is a null pointer, the realloc function
  31.     behaves like the malloc function for the specified size.  Otherwise,
  32.     if ptr does not match a pointer earlier returned by the calloc ,
  33.     malloc , or realloc function, or if the space has been deallocated by
  34.     a call to the free or realloc function, the behavior is undefined.  If
  35.     the space cannot be allocated, the object pointed to by ptr is
  36.     unchanged.  If size is zero and ptr is not a null pointer, the object
  37.     it points to is freed.
  38.  
  39.     Returns
  40.  
  41.     The realloc function returns either a null pointer or a pointer to
  42.     the possibly moved allocated space.
  43.  
  44. There is no mention that the allocated space can be moved only if the
  45. size is increased.
  46.  
  47. >revealed that the realloc is moving my pointer!  am I mistaken about the 
  48. >standards, or is this a special case I'm not aware of?
  49.  
  50. You're definitely mistaken about the C language standard.
  51.  
  52. Dan
  53. --
  54. Dan Pop
  55. CERN, CN Division
  56. Email: danpop@mail.cern.ch 
  57. Mail:  CERN - PPE, Bat. 31 R-004, CH-1211 Geneve 23, Switzerland
  58.